package com.gxa.controller.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor{
    /**
     * 在进入控制器之前执行
     * 如果是返回false就阻止控制器执行
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest,
                             HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("obj:"+o);
        System.out.println("===preHandle===");
        return true;
    }
    /**
     * 控制器执行完成，进入到jsp之前
     * 作用：
     *   日志记录
     *   敏感词语过滤
     * */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest,
                           HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        String word = modelAndView.getModel().get("model").toString();
        String newWord = word.replace("瓜娃子","***");
        modelAndView.getModel().put("model",newWord);
        System.out.println("访问的页面："+modelAndView.getViewName());
        System.out.println("===postHandle===");
    }

    /**
     * 在整个jsp成之后执行，
     * 作用：记录过程中出现的异常，可以记录到日志文件中。
     * */
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        //无论是否出现异常，都记录在日志中。
        System.out.println("===afterCompletion==="+e.getMessage());
    }
}
